package com.itita.ww2.core;

import com.itita.ww2.data.AllianceConfig;
import com.itita.ww2.data.ArmyConfig;
import com.itita.ww2.data.BuildingConfig;
import com.itita.ww2.data.CityConstructionCfg;
import com.itita.ww2.data.GameConfig;
import com.itita.ww2.data.GuideConfig;
import com.itita.ww2.data.ItemConfig;
import com.itita.ww2.data.LotteryConfig;
import com.itita.ww2.data.MissionConfig;
import com.itita.ww2.data.PlayerConfig;
import com.itita.ww2.data.WorldMapConfig;
import com.itita.ww2.data.shop.PaymentConfig;
import com.itita.ww2.data.shop.ShopConfig;
import com.itita.ww2.utils.FileUtils;
import com.itita.ww2.utils.FileUtils.ValueType;
import com.smartfoxserver.v2.entities.data.ISFSObject;

public class GlobalData {
	/*
	 * 路径
	 */
	public static String extPath; // 服务端 Extension 路径 最后有/
	public static final String cfgPath = "../../www/ww2/assets/conf"; // 配置保存路径

	// 
	// private static String path; // 实际读取路径
	/**
	 * 初始化各种数据
	 */
	public static final void load(String extPath, boolean override) throws Exception {
		GlobalData.extPath = extPath;
		// ServerConfig
		loadGameCfg(override);
		loadMissionCfg(override);
		loadPlayerCfg(override);
		loadAllianceCfg(override);
		loadShopCfg(override);
		loadArmyCfg(override);
		loadItemCfg(override);
		loadBuildingCfg(override);
		loadWorldMapCfg(override);
		loadGuideCfg(override);
		loadCityConstruction(override);
		loadLotteryCfg(override);
	}

	/**
	 *  游戏配置
	 */
	private static final void loadGameCfg(boolean override) {
		if (GameConfig.isLoaded && !override) {
			return;
		}
		String folderPath = extPath + cfgPath + "/" + "game";
		GameConfig.init(FileUtils.readSFSObjectFromFolder(folderPath, ValueType.Object));
	}

	// 读取关卡信息
	private static final void loadMissionCfg(boolean override) {
		if (MissionConfig.isLoaded && !override) {
			return;
		}
		String folderPath = extPath + cfgPath + "/" + "mission";
		MissionConfig.init(FileUtils.readSFSObjectFromFolder(folderPath, ValueType.Object));
	}

	/**
	 *  用户配置
	 */
	private static final void loadPlayerCfg(boolean override) {
		if (PlayerConfig.isLoaded && !override) {
			return;
		}
		String folderPath = extPath + cfgPath + "/" + "player";
		PlayerConfig.init(FileUtils.readSFSObjectFromFolder(folderPath, ValueType.Object));
	}

	private static final void loadAllianceCfg(boolean override) {
		if (AllianceConfig.isLoaded && !override) {
			return;
		}
		String folderPath = extPath + cfgPath + "/" + "alliance";
		AllianceConfig.init(FileUtils.readSFSObjectFromFolder(folderPath, ValueType.Object));
	}

	/**
	 * 商店配置
	 */
	private static final void loadShopCfg(boolean override) {
		if (ShopConfig.isLoaded && !override) {
			return;
		}
		String folderPath = extPath + cfgPath + "/" + "shop";
		ISFSObject obj = FileUtils.readSFSObjectFromFolder(folderPath, ValueType.Object);
		ShopConfig.init(obj);
		PaymentConfig.init(obj);
	}

	/**
	 * 加载兵种配置
	 */
	private static final void loadArmyCfg(boolean override) {
		if (ArmyConfig.isLoaded && !override) {
			return;
		}
		String folderPath = extPath + cfgPath + "/" + "army";
		ArmyConfig.init(FileUtils.readSFSObjectFromFolder(folderPath, ValueType.Object));
	}

	/**
	 * 加载物品配置
	 */
	private static final void loadItemCfg(boolean override) {
		if (ItemConfig.isLoaded && !override) {
			return;
		}
		String folderPath = extPath + cfgPath + "/" + "item";
		ItemConfig.init(FileUtils.readSFSObjectFromFolder(folderPath, ValueType.Object));
	}

	/**
	 * 加载建筑配置
	 */
	private static final void loadBuildingCfg(boolean override) {
		if (BuildingConfig.isLoaded && !override) {
			return;
		}
		String folderPath = extPath + cfgPath + "/" + "building";
		BuildingConfig.init(FileUtils.readSFSObjectFromFolder(folderPath, ValueType.Object));
	}

	/**
	 * 加载新手引导
	 */
	private static final void loadGuideCfg(boolean override) {
		if (GuideConfig.isLoaded && !override) {
			return;
		}
		String folderPath = extPath + cfgPath + "/" + "guide";
		GuideConfig.init(FileUtils.readSFSObjectFromFolder(folderPath, ValueType.Object));
	}

	/**
	 *  大地图数据
	 */
	public static final void loadWorldMapCfg(boolean override) {
		if (WorldMapConfig.isLoaded && !override) {
			return;
		}
		String folderPath = extPath + cfgPath + "/" + "worldmap";

		WorldMapConfig.init(FileUtils.readSFSObjectFromFile(folderPath + "/config.json", ValueType.Object),
				FileUtils.readSFSObjectFromFile(folderPath + "/blocklist.json", ValueType.Array),
				FileUtils.readSFSObjectFromFolder(folderPath + "/" + "citylist", ValueType.Array),
				FileUtils.readSFSObjectFromFolder(folderPath + "/" + "baselist", ValueType.Array),
				FileUtils.readSFSObjectFromFolder(folderPath + "/" + "basemap", ValueType.Object));
	}

	private static final void loadCityConstruction(boolean override) throws Exception {
		if (CityConstructionCfg.isLoaded && !override) {
			return;
		}
		String folderPath = extPath + cfgPath + "/" + "city_construction";
		CityConstructionCfg.init(FileUtils.readSFSObjectFromFolder(folderPath, ValueType.Object));
	}

	private static final void loadLotteryCfg(boolean override) {
		if (LotteryConfig.isLoaded && !override) {
			return;
		}
		String folderPath = extPath + cfgPath + "/" + "lottery";
		LotteryConfig.init(FileUtils.readSFSObjectFromFolder(folderPath, ValueType.Object));
	}
}
